package com.example.hotelsystem.service.impl;

import code.CodeError;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hotelsystem.entity.*;
import com.example.hotelsystem.entity.vo.RoomSearchVO;
import com.example.hotelsystem.mapper.HotelorderMapper;
import com.example.hotelsystem.mapper.RoomMapper;
import com.example.hotelsystem.page.Pagesize;
import com.example.hotelsystem.result.Result;
import com.example.hotelsystem.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hotelsystem.vo.comment.CommentViewVO;
import com.example.hotelsystem.vo.room.RoomViewVO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lee
 * @since 2020-10-14
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {

    Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    CityService cityService;
    @Autowired
    TDictService tDictService;
    @Autowired
    HotelNameService hotelNameService;
    @Autowired
    HotelorderMapper hotelorderMapper;
    @Autowired
    RoomTypeService roomTypeService;
    @Autowired
    HotelorderService hotelorderService;
    @Autowired
    RoomService roomService;
    @Autowired
    FloorService floorService;
    @Autowired
    private RoomTypeImageService roomTypeImageService;
    @Autowired
    RoomTypeService RoomTypeService;
    @Autowired
    TCommentService tCommentService;
    @Autowired
    UserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result searchRoomList(RoomSearchVO roomSearchVO){
        QueryWrapper<Room> query = new QueryWrapper<>();

        query.eq("is_delete",0);

        if(roomSearchVO.getCheckinTime()!=null&&roomSearchVO.getCheckoutTime()!=null){
            List<Hotelorder> list =hotelorderMapper.BookableList(roomSearchVO.getCheckinTime(),roomSearchVO.getCheckoutTime());
            for(Hotelorder order:list){
                query.ne("id",order.getRoomId());
            }
        }

        if(roomSearchVO.getStatus()!=null){
            query.eq("status",roomSearchVO.getStatus());
        }

        if(roomSearchVO.getRoomTypeId()!=null){
            query.eq("roomtype_id",roomSearchVO.getRoomTypeId());
        }

        if(roomSearchVO.getFloorId()!=null){
            query.eq("floor_id",roomSearchVO.getFloorId());
        }

        if(roomSearchVO.getHotelId()!=null){
            query.eq("hotel_id",roomSearchVO.getHotelId());
        }

        if(roomSearchVO.getCityId()!=null){
            query.eq("city_id",roomSearchVO.getCityId());
        }
        
        Page page = new Page(roomSearchVO.getCurrentPage(), Pagesize.pageSize1);
        IPage<Room> rooms=roomService.page(page,query);
        for(Room item : rooms.getRecords()){
            RoomType rt = RoomTypeService.getById(item.getRoomtypeId());
            item.setRoomTypeName(rt.getType());
            Floor f = floorService.getById(item.getFloorId());
            item.setFloorName(f.getName());
            HotelName hn = hotelNameService.getById(item.getHotelId());
            item.setHotelName(hn.getHotelName());
            City city = cityService.getById(item.getCityId());
            item.setCityName(city.getCityName());
            item.setStatusText(tDictService.getDictText(item.getStatus(),"hotelRoomStatus"));
        }
        return Result.success(rooms);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result screenRoomList(Integer currentPage,Integer status){
        Page page = new Page(currentPage, Pagesize.pageSize1);
        QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        IPage<Room> rooms=roomService.page(page,queryWrapper);
        if (rooms!=null){
            for(Room room:rooms.getRecords()){
                Long typeId = room.getRoomtypeId();
                RoomType rt = RoomTypeService.getById(typeId);
                room.setRoomType(rt!=null?rt:"房间类型丢失了，😭");
            }
            return Result.success("房间列表获取成功",rooms);
        }else {
            return Result.error(CodeError.ROOM_LIST_NULL,"没有找到房间列表");
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result roomList(Integer currentPage){
        Page page = new Page(currentPage, Pagesize.pageSize1);
        IPage<Room> rooms=roomService.page(page);
        if (rooms!=null){
            for(Room room:rooms.getRecords()){
                Long typeId = room.getRoomtypeId();
                RoomType rt = RoomTypeService.getById(typeId);
                room.setRoomType(rt!=null?rt:"房间类型丢失了，😭");
            }
            return Result.success("房间列表获取成功",rooms);
        }else {
            return Result.error(CodeError.ROOM_LIST_NULL,"没有找到房间列表");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result roomVIew(Long id){
        RoomViewVO roomViewVO = new RoomViewVO();
        Room room = roomService.getById(id);
        if(room==null){
            return Result.error(CodeError.ROOM_VIEW_FAIL,"房间详情获取失败");
        }
        BeanUtils.copyProperties(room,roomViewVO);
        Floor f = floorService.getById(roomViewVO.getFloorId());
        roomViewVO.setFloorName(f.getName());
        roomViewVO.setFloorNum(f.getNumber());

        RoomType rt = roomTypeService.getById(roomViewVO.getRoomtypeId());
        roomViewVO.setRoomTypeName(rt.getType());
        roomViewVO.setPrice(rt.getPrice());

        City c = cityService.getById(roomViewVO.getCityId());
        roomViewVO.setCityName(c.getCityName());

        HotelName hn = hotelNameService.getById(roomViewVO.getHotelId());
        roomViewVO.setHotelName(hn.getHotelName());


        QueryWrapper<RoomTypeImage> query = new QueryWrapper<>();
        query.eq("room_type_id",roomViewVO.getRoomtypeId());
        List<RoomTypeImage> images = roomTypeImageService.list(query);
        List<String> imageList = new ArrayList<>();
        for(RoomTypeImage item : images){
            imageList.add(item.getImage());
        }
        roomViewVO.setImageList(imageList);


//        QueryWrapper<TComment> commentQuery = new QueryWrapper<>();
//        commentQuery.eq("room_id",room.getId());
//        List<TComment> comments = tCommentService.list(commentQuery);
//        List<CommentViewVO> commentList = new ArrayList<>();


        roomViewVO.setStatusText(tDictService.getDictText(room.getStatus(),"hotelRoomStatus"));

//        int count = 0;
//        double starSum = 0;
//        for(TComment item : comments){
//            CommentViewVO vo = new CommentViewVO();
//            BeanUtils.copyProperties(item,vo);
//
//            User user = userService.getById(item.getUserId());
//            vo.setAvatar(user.getAvatar());
//            vo.setUserName(user.getUsername());
//            commentList.add(vo);
//
//            starSum += item.getStar();
//            count++;
//
//        }
//        roomViewVO.setStar(starSum/count);
//        roomViewVO.setCommentList(commentList);
        return Result.success(roomViewVO);

    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result BookableRoomList(Integer currentPage, Date checkinTime, Date checkoutTime){
        List<Hotelorder> list =hotelorderMapper.BookableList(checkinTime,checkoutTime);
        QueryWrapper<Room> query1 = new QueryWrapper<>();
        for(Hotelorder order:list){
            query1.ne("id",order.getRoomId());
        }
        Page page = new Page(currentPage, Pagesize.pageSize1);
        IPage<Room> rooms=roomService.page(page,query1);
        if (rooms!=null){
            for(Room room:rooms.getRecords()){
                Long typeId = room.getRoomtypeId();
                RoomType rt = RoomTypeService.getById(typeId);
                room.setRoomType(rt!=null?rt:"房间类型丢失了，😭");
            }
            logger.info(new Date()+"房间列表获取成功");
            return Result.success("房间列表获取成功",rooms);

        }else {
            return Result.error(CodeError.ROOM_LIST_NULL,"没有找到房间列表");
        }
    }

}
